<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="物业" prop="ohid">
          <el-select  v-model="queryParams.ohid" placeholder="请选择物业" clearable :style="{width: '100%'}">
            <el-option  v-for="property in propertys" :key="property.pmid" :label="property.propertyName" :value="property.pmid" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用类型" prop="ttid">
          <el-select  v-model="queryParams.ttid" placeholder="请选择费用类型" clearable :style="{width: '100%'}">
            <el-option  v-for="tradingType in tradingTypes" :key="tradingType.ttid" :label="tradingType.typeName" :value="tradingType.ttid" ></el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
<!--      <template>-->
<!--        <div>-->
<!--          <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">-->
<!--            <el-form-item label="日期范围" prop="field102">-->
<!--              <el-date-picker type="daterange" v-model="formData.field102" format="yyyy-MM-dd"-->
<!--                              value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期"-->
<!--                              range-separator="至" clearable></el-date-picker>-->
<!--            </el-form-item>-->
<!--            <el-form-item size="large">-->
<!--              <el-button type="primary" @click="submitForm">提交</el-button>-->
<!--              <el-button @click="resetForm">重置</el-button>-->
<!--            </el-form-item>-->
<!--          </el-form>-->
<!--        </div>-->
<!--      </template>-->
<!--      <script>-->
<!--        export default {-->
<!--          components: {},-->
<!--          props: [],-->
<!--          data() {-->
<!--            return {-->
<!--              formData: {-->
<!--                field102: null,-->
<!--              },-->
<!--              rules: {-->
<!--                field102: [],-->
<!--              },-->
<!--            }-->
<!--          },-->
<!--          computed: {},-->
<!--          watch: {},-->
<!--          created() {},-->
<!--          mounted() {},-->
<!--          methods: {-->
<!--            submitForm() {-->
<!--              this.$refs['elForm'].validate(valid => {-->
<!--                if (!valid) return-->
<!--                // TODO 提交表单-->
<!--              })-->
<!--            },-->
<!--            resetForm() {-->
<!--              this.$refs['elForm'].resetFields()-->
<!--            },-->
<!--          }-->
<!--        }-->

<!--      </script>-->
      <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="poid" />
      <el-table-column label="物业名字" align="center" prop="propertyManagement.propertyName" />
      <el-table-column label="费用类型" align="center" prop="tradingType.typeName" />
      <el-table-column label="车牌号" align="center" prop="carsMsg.licenseNumber" />
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="170px" />
      <el-table-column label="是否支付" align="center" prop="orderStatus" >
        <template slot-scope="scope">
          {{ scope.row.orderStatus===0?'未支付':scope.row.orderStatus===1?'已支付':'状态有误' }}
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="updateTime" width="170px" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停车订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="唯一编码,前台使用" prop="code">
          <el-input v-model="form.code" placeholder="请输入唯一编码,前台使用" />
        </el-form-item>
        <el-form-item label="是否删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="物业id" prop="ohid">
          <el-input v-model="form.ohid" placeholder="请输入物业id" />
        </el-form-item>
        <el-form-item label="费用类型id" prop="ttid">
          <el-input v-model="form.ttid" placeholder="请输入费用类型id" />
        </el-form-item>
        <el-form-item label="车辆信息id" prop="cmid">
          <el-input v-model="form.cmid" placeholder="请输入车辆信息id" />
        </el-form-item>
        <el-form-item label="费用金额" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入费用金额" />
        </el-form-item>
        <el-form-item label="支付类型" prop="ptid">
          <el-input v-model="form.ptid" placeholder="请输入支付类型" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder ,listProperty ,listTradingType } from "@/api/modules/sys-property-wechat/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停车订单表格数据
      orderList: [],
      //物业数据
      propertys: [],
      //费用类型
      tradingTypes: [],
      //时间模糊查询参数
      daterangeCreateTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        status: null,
        deleted: null,
        ohid: null,
        ttid: null,
        cmid: null,
        orderPrice: null,
        orderStatus: null,
        ptid: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "唯一编码,前台使用不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        deleted: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        ohid: [
          { required: true, message: "物业id不能为空", trigger: "blur" }
        ],
        ttid: [
          { required: true, message: "费用类型id不能为空", trigger: "blur" }
        ],
        cmid: [
          { required: true, message: "车辆信息id不能为空", trigger: "blur" }
        ],
        orderPrice: [
          { required: true, message: "费用金额不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "是否支付不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询停车订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listProperty().then(response=>{
        this.propertys = response.rows;
      });
      listTradingType().then(response=>{
        this.tradingTypes = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        poid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        ohid: null,
        ttid: null,
        cmid: null,
        orderPrice: null,
        orderStatus: null,
        ptid: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.poid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加停车订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const poid = row.poid || this.ids
      getOrder(poid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改停车订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.poid != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const poids = row.poid || this.ids;
      this.$modal.confirm('是否确认删除停车订单编号为"' + poids + '"的数据项？').then(function() {
        return delOrder(poids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
